<?php

namespace backend\controllers;

use backend\controllers\RbacController;
use backend\models\ReportForm;
use yii;
use yii\base\Action;
use backend\models\TmeOrderinfo;
use backend\models\TbaMemberinfo;
use backend\models\TmeMerchandiseinfo;
use backend\models\TmeUnitinfo;
use backend\models\TmeOrderdetailsinfo;
use yii\db\Query;
/**
 * #name=订单统计报表#
 * @author Administrator
 *
 */
class ReportController extends RbacController {
	public $enableCsrfValidation = false;
	
	public  $layout = 'ace';
	
	/**
	 * #operation=默认操作#
	 * @return Ambigous <string, string>
	 */
	public function actionIndex()
	{
		//实例化模型
		$model= new ReportForm(['scenario'=>'inquire']);
		
		//视图传送
		return $this->render('report',[
			'model'=>$model
		]);
		
	}	
	

	
	/**
	 * #operation=表格数据展现#
	 */
	public function actionTable()
	{
		
		
		
		if (\Yii::$app->request->post('action')=='action'){
			
			$start_time=\yii::$app->request->post('start_time');  //开始时间
			$end_time=\yii::$app->request->post('end_time');     //结束时间
			//时间初始化
			if (empty($start_time) or empty($end_time)){
				$season = ceil((date('n'))/3);
				$start_time=date('Y-m', mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
				$end_time=date('Y-m', mktime(23,59,59,$season*12,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')));
			
				}
				$end=date("Y-m",strtotime($end_time));
				
				//月份数组
				$arry=[];
				//查询时间差值内的各各月
			while ($start_time<$end)
			{
				$arry[]=$start_time;
				$start_time=date("Y-m",strtotime("+1 months", strtotime($start_time)));
			}
			
				$arry[]=$end_time;			
				$totale=$arry[count($arry)-1];
				$totals=$arry[0];
			
			if (count($arry)>1){
			$totale1=date('Y-m',strtotime($arry[count($arry)-2]));
			
			$totals1=date('Y-m',strtotime($arry[1]));
			}	
		//判断是否起始时间是否是在同一个月
		$monthif  = floor((strtotime($totale)-strtotime($totals))/ 60 / 60 / 24 / 30);
		
		if ($monthif == 0){
			unset($arry);
				
			//时间段内有值的去重后日期
			$times = (new \yii\db\Query())
			->select('OrderTime')
			->distinct()
			->from('tme_orderinfo')
			->where(['>=','OrderTime',$start_time])
			->andWhere(['<=','OrderTime',$end_time])
			->all();
				
		
			foreach ($times as $c){
		
				$arry[]=$c['OrderTime'];
		
				//订单数量
				$show []= (new \yii\db\Query())
				->select('*')
				->from('tme_orderinfo')
				->where(['like','OrderTime',$c])
				->count();
		
				//订单金额
				$money = (new \yii\db\Query())
				->select(['TotalMoney'])
				->from('tme_orderinfo')
				->where(['like','OrderTime',$c])
				->all();
				$moneys='';
				foreach ($money as $i){
				  $moneys +=$i['TotalMoney'];
				}
				$show1[]=$moneys;
		
		
			}
		
		
				
		}else{
		
			$where="OrderTime>='".$totals."' and OrderTime<='".$totale."'";
		
			$datas = (new \yii\db\Query())
			->select(['DATE_FORMAT(OrderTime,"%Y-%m") month,sum(TotalMoney),count(*) as num,count(case BillStatus when 4 then 1 end) as checked,count(case BillStatus when 1 then 1 end) as completed,count(case BillStatus when 2 then 1 end) as shippingnow,count(case BillStatus when 3 then 1 end)as delivered'])
			->from('tme_orderinfo')
			->where($where)
			->groupBy('month')
			->all();
		
			$arry=[];
			foreach($datas as $v){
				$show[]=$v['num'];
				$show1[]=$v['sum(TotalMoney)'];
				$arry[]=$v['month'];
		
			}
			
	    }	
	
}
	if (\Yii::$app->request->post('list')=='0'){
		
		 $start=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
		 $end=date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
		
		 $where="OrderTime>='".$start."' and OrderTime<='".$end."'";
		 
		 $datas = (new \yii\db\Query())
		 ->select('OrderTime')
		 ->distinct()
		 ->from('tme_orderinfo')
		 ->where($where)
		 ->all();
		 
		 $arry=[];
		 foreach($datas as $v){
		 	//订单时间
		 	$times=$v['OrderTime'];
		 	
		 	//订单总个数
		 	$show []=(new \yii\db\Query())
		 	->select('*')
		 	->from('tme_orderinfo')
		 	->where(['OrderTime'=>$times])
		 	->count();
		 		
		 	//订单总金额
		 	$mony = (new \yii\db\Query())
		 	->select(['TotalMoney'])
		 	->from('tme_orderinfo')
		 	->where(['OrderTime'=>$times])
		 	->all();
		 	$monys='';
		 	foreach ($mony as $i){
		 		$monys +=$i['TotalMoney'];
		 	}
		 	$arry[]=$times;
		 	$show1[]=$monys;
		 	
		 	
		 
		 }
		
		
	}	
	
	if (\Yii::$app->request->post('list')=='1'){
	
		$start=date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
		$end=date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
	
		$where="OrderTime>='".$start."' and OrderTime<='".$end."'";
		 
		 $datas = (new \yii\db\Query())
		 ->select('OrderTime')
		 ->distinct()
		 ->from('tme_orderinfo')
		 ->where($where)
		 ->all();
		 
		 $arry=[];
		 foreach($datas as $v){
		 	//订单时间
		 	$times=$v['OrderTime'];
		 	
		 	//订单总个数
		 	$show []=(new \yii\db\Query())
		 	->select('*')
		 	->from('tme_orderinfo')
		 	->where(['OrderTime'=>$times])
		 	->count();
		 		
		 	//订单总金额
		 	$mony = (new \yii\db\Query())
		 	->select(['TotalMoney'])
		 	->from('tme_orderinfo')
		 	->where(['OrderTime'=>$times])
		 	->all();
		 	$monys='';
		 	foreach ($mony as $i){
		 		$monys +=$i['TotalMoney'];
		 	}
		 	$arry[]=$times;
		 	$show1[]=$monys;
		 	
		 	
		 
		 }
	}
	
	if (\Yii::$app->request->post('list')=='2'){
		$year=date("Y",time());
		$start=$year."-01-01";
		$end=$year."-12-31";
	
		$where="OrderTime>='".$start."' and OrderTime<='".$end."'";
			
		$datas = (new \yii\db\Query())
		->select(['DATE_FORMAT(OrderTime,"%Y-%m") month,sum(TotalMoney),count(*) as num,count(case BillStatus when 4 then 1 end) as checked,count(case BillStatus when 1 then 1 end) as completed,count(case BillStatus when 2 then 1 end) as shippingnow,count(case BillStatus when 3 then 1 end)as delivered'])
		->from('tme_orderinfo')
		->where($where)
		->groupBy('month')
		->all();
			
		$arry=[];
		foreach($datas as $v){
			$show[]=$v['num'];
			$show1[]=$v['sum(TotalMoney)'];
			$arry[]=$v['month'];
	
		}
	}
	
		

		if(empty($show) and empty($show1)){
			$show[]='';
			$show1[]='';
			$arry[]='您搜索的时间段没有订单!';
		}
	
	$arry=array_values($arry);
	$title=['订单数','订单金额'];
	$monent=$arry;
	$names=[
	['type'=>'value','name'=>$title[0],'axisLabel'=>['formatter'=>'{value} 个']],
	['type'=>'value','name'=>$title[1],'axisLabel'=>['formatter'=>'{value} 元']]
	];
	//订单数组展现
	$arry1=$show;
	//订单金额数组展现
	$arry2=$show1;
	$series=[
	['name'=>$title[0],'type'=>'bar','data'=>$arry1],
	['name'=>$title[1],'type'=>'line','yAxisIndex'=>1,'data'=>$arry2]
	];
	$aa=[
	$title,
	$monent,
	$names,
	$series
	];
	
	echo  json_encode($aa);  //将数据转变为json字符串
	
	}
	
	

	/**
	 * #operation=订单统计数据查询#
	 */
	
	//json数据   订单统计数据查询
	public function actionAjxapie(){
		
			//if(\yii::$app->request->isPost){
			 
			$start=intval(\yii::$app->request->post('start'));    //起始数据条数
			$length=intval(\yii::$app->request->post('length'));  //需要显示的数据条数
			$draw=intval(\yii::$app->request->post('draw'));
		
			$column=intval(\yii::$app->request->post('order')[0]['column']);   //排序的列
			$dir = \yii::$app->request->post('order')[0]['dir'];     //排序方式
			$start_time=\yii::$app->request->post('start_time');  //开始时间
		   $end_time=\yii::$app->request->post('end_time');     //结束时间
		   
		   
		   
		   if (\Yii::$app->request->post('list')=='2'){
		   	$year=date("Y",time());
		   	$start_time=$year."-01-01";
		   	$end_time=$year."-12-31";
		  
		   }
		   if (\Yii::$app->request->post('list')=='1'){
		  
		   	$start_time=date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
		   	$end_time=date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
		   	
		   
		   }
		   if (\Yii::$app->request->post('list')=='0'){
		   
		   	$start_time=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
		   	$end_time=date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
		   
		   	 
		   }
		   
		   
			if ($start_time!=0 or $end_time!=0){
				$where1 ="OrderTime>='".$start_time."' and OrderTime<='".$end_time."'";
				
			}else{
				$where1='';
			} 
		
		
		
			
			$columArry=[
		
			"0"=>"BillCode",
			"1"=>"RecMan",
			"2"=>"DeliveryName",
			"3"=>"TotalMoney"
			];
		
		
			//构建排序条件
			
				$cloumName=$columArry[$column];
			
			
			//根据搜索值时构建查询条件
			if(\yii::$app->request->post('search')['value']!=""){
		
				$value=\yii::$app->request->post('search')['value'];
				
				
					$where = "OrderTime like '%".$value."%'";
		
			}else{
		
				$where="";
			}
		
			
			//根据条件查询数据
			
			$report = (new \yii\db\Query())
			->select('OrderTime')
			->distinct()
			->from('tme_orderinfo')
			->where($where1)			
			->andWhere($where)
			->orderBy($cloumName." ".$dir)
			->limit($length)
			->offset($start)
			->all();
			
			
			
			$total =count($report);      //查询数据总数
			
			$arr=[];
			foreach ($report as $v){
					
			//订单时间	
			$times=$v['OrderTime'];	
			 
			//订单总个数	
			$totale=(new \yii\db\Query())
			->select('*')
			->from('tme_orderinfo')
			->where(['OrderTime'=>$times])
			->count();
			
			//订单总金额
			$mony = (new \yii\db\Query())
			->select(['TotalMoney'])
			->from('tme_orderinfo')
			->where(['OrderTime'=>$times])
			->all();
			$monys='';
			foreach ($mony as $i){
				$monys +=$i['TotalMoney'];
			}
			
			//待审核订单
			
			$notorder=(new \yii\db\Query())
			->select('BillStatus')
			->from('tme_orderinfo')
			->where(['OrderTime'=>$times])
			->andWhere(['BillStatus'=>'0'])
			->count();
			
			
				
				 
				$temp=[];
				
				$temp['BillCode']=$times;
				$temp['RecMan']=$totale;
				$temp['Delivery']=$monys;
				$temp['TotalMoney']=$notorder;
				
				
				 
				$arr[]=$temp;
		
			}
			 
			//构造返回数据
			$tmp=[
			"draw"=>$draw,
			"recordsTotal"=>$total,     //数据综述
			"recordsFiltered"=>$total,  //数据总数
			"data"=>$arr                //数据
			 
			];
			 
			echo  json_encode($tmp);  //将数据转变为json字符串
		
		}
		
	
		
	//}
}

?>